#include<cstdio>
#include<algorithm>
#include<string.h>
#include <string.h>
#include <math.h>
using namespace std;
 
typedef long long ll;
int dp[105][60][60];
char M[105][12];
bool is[105][12];
int can[150];
const int Mod = 1e8;
int n, m, t = 0;
void init()
{
    for(int i = 0; i < (1<<10); i++)
    {
        if((i&(i<<1)) || (i&(i<<2))) continue; //一行中相邻的战队不会攻击
        can[++t] = i;
    }
    return;
}
int check(int x, int state) //看此行state是否为符合题意的状态
{
    int cnt = 0;
    if( state >= (1<<m) ) return -1;
    for(int i = 1; i <= m; i++)
    {
        if( (1<<(i-1))& state ) cnt++;
        if(!is[x][i]) //x行第i个是山地则state(m-i)位不能放战队
        {
            if( (1<<(m-i)) & state) return -1;
        }
    }
    return cnt;
}
int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        init();
        for(int i = 1; i <= n; i++)
        {
            scanf("%s", M[i]+1);
            for(int j = 1; j <= m; j++)
            {
                if(M[i][j] == 'P') is[i][j] = 1;//确定初始状态
                else is[i][j] = 0;
            }
        }
        int ans = 0, now;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= t; j++)
            {
                int cnt = check(i, can[j]);
                if(cnt!=-1) {
                    for(int k = 1; k <= t; k++)
                    {
                        now = 0;
                        if(!(can[k] & can[j])) { //与i-1行比较
                            dp[i][can[j]][can[k]] = cnt;
                            for(int q = 1; q <= t; q++)
                            {
                                if(i > 1) {
                                    if(!(can[j]&can[q])) //与i-2行比较
                                        now = max(now, dp[i-1][can[k]][can[q]]);
                                }
                            }
                        }
 
                        dp[i][can[j]][can[k]] += now;
                        if(i == n) ans = max(ans, dp[i][can[j]][can[k]]);
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}